package edu.biwu.service.impl;

import edu.biwu.dao.ProductDao;
import edu.biwu.po.Product;
import edu.biwu.service.ProductService;
import edu.biwu.utils.TransactionManager;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Setter
@Service
public class ProductServiceImpl implements ProductService {
    //通过setter方法在spring中注入dao
    @Autowired
    private ProductDao productDao; //@Autowired注解可以省略setter方法 注入后可以直接使用productDao 相当于ProductDao productDao = new productDaoImpl;

    @Autowired
    private TransactionManager transactionManager;
//  例如:  public void setProductDao(ProductDao productDao) {
//        this.productDao = productDao;
//    }

    @Override
    public void updateProduct(Product product) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.updateProduct(product);
//            transactionManager.commitTansaction();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollbackTransaction();
//        } finally {
//            transactionManager.releaseConnection();
//        }
        productDao.updateProduct(product);
    }

    public void deleteProduct(int pid) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.deleteProduct(pid);
//            transactionManager.commitTansaction();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollbackTransaction();
//        } finally {
//            transactionManager.releaseConnection();
//        }
        productDao.deleteProduct(pid);
    }

    @Override
    public void saveProduct(Product product) {
//        try {
//            transactionManager.beginTransaction();
//            productDao.saveProduct(product);
//            transactionManager.commitTansaction();
//        } catch (Exception e) {
//            e.printStackTrace();
//            transactionManager.rollbackTransaction();
//        } finally {
//            transactionManager.releaseConnection();
//        }
        productDao.saveProduct(product);
    }

    @Override
    public List<Product> findAll() {
        return productDao.findAll();
    }
}
